package com.zj.juc.study.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName VolatileNoAtomicDemo
 * @Description volatile没有原子性demo
 * @createTime 2025年01月02日 21:22
 */
public class VolatileNoAtomicDemo {
    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    myNumber.addPlusPlus();
                }
            },String.valueOf(i)).start();
        }
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(myNumber.number+"the last");
    }
}

class MyNumber{
    volatile int number;

    public  void addPlusPlus(){
        number++;
    }
}
